﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Beetle.Web.Composite;
using Beetle.Web.Extensions;
using System.Web.UI;
using System.Globalization;

namespace Beetle.Web.UI.WebControls
{
    public class TowWayBindExtension : ExtensionBase
    {
        public override void Attach(object owner)
        {
            System.Web.UI.Control control = owner as System.Web.UI.Control;

            if (control != null)
            {
                control.DataBinding += new EventHandler(control_DataBinding);
            }
        }

        void control_DataBinding(object sender, EventArgs e)
        {
            System.Web.UI.Control ctrl = sender as System.Web.UI.Control;

            if (ctrl != null)
            {
                object obj = ctrl.GetDataItem();

                if (obj != null)
                {
                    // System.Web.UI.IDataItemContainer container = obj as System.Web.UI.IDataItemContainer;
                    // Convert.ToString(DataBinder.Eval(container, this.Expression, this.Format), CultureInfo.CurrentCulture);
                    // DataBinder.Eval(container, this.Expression, this.Format)
                    IDataItemContainer container = obj as IDataItemContainer;
                    if (container != null && container.DataItem != null)
                    {
                        Type senderType = sender.GetType();
                        System.Reflection.PropertyInfo property = senderType.GetProperty(this.Property);

                        if (property != null && property.CanWrite)
                        {
                            try
                            {
                                if (property.PropertyType == typeof(String))
                                {
                                    string strValue = DataBinder.Eval(container.DataItem, this.Expression, this.Format);
                                    property.SetValue(sender, strValue, null);
                                }
                                else
                                {
                                    property.SetValue(sender, Convert.ChangeType(DataBinder.Eval(container.DataItem, this.Expression), property.PropertyType), null);
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
        }

        public override void Detach(object owner)
        {
            System.Web.UI.Control control = owner as System.Web.UI.Control;

            if (control != null)
            {
                control.DataBinding -= new EventHandler(control_DataBinding);
            }
        }

        public string Expression
        {
            get;
            set;
        }

        public string Format
        {
            get;
            set;
        }

        public string Property
        {
            get;
            set;
        }

        private bool _canExtract = true;
        public bool CanExtract
        {
            get
            {
                return this._canExtract;
            }
            set
            {
                this._canExtract = value;
            }
        }
    }
}
